package cn.stronglink.asset.module.assetmanage.using.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.UsingApply;
import cn.stronglink.asset.model.UsingApplyDetail;
import cn.stronglink.asset.module.assetmanage.itasset.service.ItassetService;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.using.service.UsingService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

@Controller
@RequestMapping(value = "using")
public class UsingController extends AbstractController {

	@Autowired
	private UsingService usingService;
	@Autowired
	private ItassetService itassetService;

	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {	
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("createBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<UsingApplyDetail> page = (Page<UsingApplyDetail>) super.getPage(params);
		Page<UsingApplyDetail> data = usingService.queryList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 新增申请领用
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "申请领用",desc="增加申请领用单", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody UsingApply apply) {
		usingService.create(apply);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 查询领用申请详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		UsingApplyDetail data = usingService.qryById(id);
		List<ItAssetVo> vo = itassetService.qryItAssetVo(id);
		modelMap.put("dataVo", vo);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 编辑领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "申请领用",desc="编辑申请领用单", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody UsingApplyDetail detail) {
		usingService.edit(detail);
		return setSuccessModelMap(modelMap, null);
	}
	/**
	 * 删除领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "申请领用",desc="删除申请领用单", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		usingService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	/**
	 * 领用申请分配
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "申请领用分配",desc="领用分配", type = OpType.UPDATE)
	@RequestMapping(value = "distribution")
	public ResponseEntity<ModelMap> distribution(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody UsingApplyDetail detail) {
		usingService.distribution(detail);
		return setSuccessModelMap(modelMap, null);
	}
	
}
